<?php
class ModelFidoPartner extends Model {
	public function addPartner($data) {
		$this->db->query("INSERT INTO ". DB_PREFIX ."partner SET partner_file='" . $this->db->escape($data['image']) .  "',partner_width= '". (int)$data['partner_width'] .  "',partner_height= '". (int)$data['partner_height'] .  "',site_url= '". $this->db->escape($data['site_url']) ."',start_date='". $this->db->escape($data['start_date']) .  "',enabled='".(int)$data['status']."'");
		$partner_id = $this->db->getLastId();
		foreach ($data['site_name'] as $language_id => $value) {
			$this->db->query("INSERT INTO " . DB_PREFIX . "partner_description SET partner_id = '" . (int)$partner_id . "', language_id = '" . (int)$language_id . "', site_name = '" . $this->db->escape($value['name']) . "'");
		}
		if (isset($data['partner_store'])) {
			foreach ($data['partner_store'] as $store_id) {
				$this->db->query("INSERT INTO " . DB_PREFIX . "partner_to_store SET partner_id = '" . (int)$partner_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
		$this->cache->delete('partner');
	}

	public function editPartner($partner_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "partner SET partner_file='" . $this->db->escape($data['image']) .  "',partner_width= '". (int)$data['partner_width'] .  "',partner_height= '". (int)$data['partner_height'] .  "',site_url= '". $this->db->escape($data['site_url']) ."',start_date='". $this->db->escape($data['start_date']) . "',enabled = '" . (int)$data['status'] . "', partner_active = '" . (int)$this->request->post['active'] . "' WHERE partner_id = '" . (int)$partner_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "partner_description WHERE partner_id = '" . (int)$partner_id . "'");
		foreach ($data['site_name'] as $language_id => $value) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "partner_description SET partner_id = '" . (int)$partner_id . "',site_name='". $this->db->escape($value['name'])."', language_id = '" . (int)$language_id . "'");
		}
		$this->db->query("DELETE FROM " . DB_PREFIX . "partner_to_store WHERE partner_id = '" . (int)$partner_id . "'");
		if (isset($data['partner_store'])) {
			foreach ($data['partner_store'] as $store_id) {
		$this->db->query("INSERT INTO " . DB_PREFIX . "partner_to_store SET partner_id = '" . (int)$partner_id . "', store_id = '" . (int)$store_id . "'");
			}
		}
		$this->cache->delete('partner');
	}

	public function deletePartner($partner_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "partner WHERE partner_id = '" . (int)$partner_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "partner_description WHERE partner_id = '" . (int)$partner_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "partner_to_store WHERE partner_id = '" . (int)$partner_id . "'");
		$this->cache->delete('partner');
	}	

	public function getPartner($partner_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "partner WHERE partner_id = '" . (int)$partner_id . "'");
		return $query->row;
	}

	public function getPartnerDescriptions($partner_id) {
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "partner_description WHERE partner_id = '" . (int)$partner_id . "'");
		foreach ($query->rows as $result) {
			$partner_description_data[$result['language_id']] = array(
				 'name'=> $result['site_name']
			);
		}
		return $partner_description_data;
	}

	public function getPartners() {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "partner p LEFT JOIN " . DB_PREFIX . "partner_description pd ON (p.partner_id = pd.partner_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY p.start_date");
		return $query->rows;
	}

	public function getTotalPartners() {
     	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "partner");
		return $query->row['total'];
	}

	public function checkActivePartner() {
     	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "partner WHERE partner_active = '1'");
		return $query->row['total'];
	}

	public function getActivePartner() {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "partner WHERE partner_active = '1'");
		return $query->row;
	}

	public function getPartnerData($partner_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "partner p LEFT JOIN " . DB_PREFIX . "partner_description pd ON (p.partner_id = pd.partner_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.partner_id = '" . (int)$partner_id . "'");
		return $query->row;
	}

	public function getPartnerResults($partner_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "partner_reactions WHERE partner_id = '" . (int)$partner_id . "'");
		return $query->rows;
	}

	public function getTotalResults($partner_id) {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "partner_reactions WHERE partner_id = '" . (int)$partner_id . "'");
		return $query->row['total'];
	}

	public function getPartnerStores($partner_id) {
		$partner_store_data = array();
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "partner_to_store WHERE partner_id = '" . (int)$partner_id . "'");
		foreach ($query->rows as $result) {
			$partner_store_data[] = $result['store_id'];
		}
		return $partner_store_data;
	}

	public function checkPartners() {
		$create_partner = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "partner` ( `partner_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `partner_file` varchar(128) NOT NULL,`partner_width` smallint(3) DEFAULT NULL,`partner_height` smallint(3) DEFAULT NULL,`site_url` varchar(128) NOT NULL,`hits` int(11) unsigned NOT NULL DEFAULT '0',`start_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',`end_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `partner_order` int(11) unsigned NOT NULL DEFAULT '1',`partner_pos` int(11) NOT NULL,`partner_pages` text,`partner_active` int(1) unsigned NOT NULL DEFAULT '1',`enabled` tinyint(1) unsigned NOT NULL DEFAULT '0',PRIMARY KEY (`partner_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";
		$this->db->query($create_partner);
		$create_partner_descriptions = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "partner_description` ( `partner_id` int(11) NOT NULL, `site_name` varchar(128) NOT NULL, `language_id` int(11) NOT NULL,PRIMARY KEY (`partner_id`,`language_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";
		$this->db->query($create_partner_descriptions);		
		$create_partner_to_store = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "partner_to_store` (`partner_id` int(11) NOT NULL, `store_id` int(11) NOT NULL, PRIMARY KEY  (`partner_id`, `store_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";
		$this->db->query($create_partner_to_store);
	}
}
?>
